package com.example.tenant.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.tenant.entity.SysOauthClientDetails;
import com.example.tenant.mapper.SysClientMapper;
import com.example.tenant.service.SysClientAdminService;
import com.example.tenant.service.SysClientService;
import com.example.tenant.service.SysDeptClientService;
import com.example.tenant.service.SysUserClientService;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.stereotype.Service;

import java.io.FileOutputStream;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 行政区划
 *
 * @author waterx
 * @date 2024-02-16 22:40:06
 */
@Service
@AllArgsConstructor
public class SysClientServiceImpl extends ServiceImpl<SysClientMapper, SysOauthClientDetails> implements SysClientService {

    private final SysClientAdminService sysClientAdminService;

    private final SysDeptClientService sysDeptClientService;

    private final SysUserClientService sysUserClientService;

    @SneakyThrows
    @Override
    public void export(Long tenantId, FileOutputStream outputStream, String filePath) {

        List<SysOauthClientDetails> clientList = this.list(new LambdaQueryWrapper<SysOauthClientDetails>().eq(SysOauthClientDetails::getTenantId, tenantId));
        List<String> clientIds = clientList.stream().map(SysOauthClientDetails::getId).collect(Collectors.toList());

        sysClientAdminService.export(clientIds, filePath);

        sysDeptClientService.export(clientIds, filePath);

        sysUserClientService.export(clientIds, filePath);


        try (ExcelWriter excelWriter = EasyExcel.write(outputStream, entityClass).build()) {
            // 创建工作表
            WriteSheet writeSheet = EasyExcel.writerSheet("sheet").build();
            // 写入数据
            excelWriter.write(clientList, writeSheet);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
